package com.huayi.website.controller.manage;

import com.huayi.website.service.MessageBoardService;
import com.huayi.website.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/manage")
@Controller
public class MMessageBoardController {
    @Autowired
    private MessageBoardService messageBoardService;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(value = "/message_list", method = RequestMethod.GET)
    public String getList(HttpServletRequest request, Model model) {
        Integer pageIndex = 1, pageSize = 10;
        if (!StringUtils.isEmpty(request.getParameter("page"))) {
            pageIndex = Integer.parseInt(request.getParameter("page"));
        }

        String title="";
        if (!StringUtils.isEmpty(request.getParameter("title"))) {
            title = request.getParameter("title");
            model.addAttribute("title",title);
        }
        var result = messageBoardService.getList(pageIndex,pageSize,"","","");
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("list", result.getList());
        model.addAttribute("totalSize", result.getTotalSize());

        model.addAttribute("menu","message_list");
        return "manage/message_list";
    }

    /**
     * 回复
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/message_reply", method = RequestMethod.POST)
    public ResultVo<String> publishPost(Model model, @RequestBody MessageReplyReq req, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String userName = session.getAttribute("userName").toString();
        if (StringUtils.isEmpty(userName)) return new ResultVo<>(500, "请登录");

        if(StringUtils.isEmpty(req.getContent())) return new ResultVo<>(500, "请填写回复备注");

        messageBoardService.messageReply(req.getId(),userName,req.getContent());

        return new ResultVo<>(0, "SUCCESS");
    }
}
